<template>
  <div id="Imgdetails">
    <transition @before-enter="beforeEnter" @enter="enter" @after-enter="afterEnter">
      <div class="ball" v-show="flagboll" ref="myball"></div>
    </transition>
    <div class="mui-card">
      <div class="mui-card-content">
        <!-- 轮播图 -->
        <swiper :lunbotuList="lunbotuList" :isfull="false"></swiper>
        <!-- /轮播图 -->
      </div>
    </div>

    <!-- 商品购买区域 -->
    <div class="mui-card">
      <div class="mui-card-header">{{pricelist.title}}</div>
      <div class="mui-card-content">
        <div class="mui-card-content-inner">
          <p class="price">
            市场价&nbsp;：
            <del class="old-price">&yen;{{pricelist.market_price}}</del>&nbsp;&nbsp;&nbsp;销售价&nbsp;：
            <span class="now-price">&yen;{{pricelist.sell_price}}</span>
          </p>
          <p class="nummer">购买数量：
            <number @mynum="number" :max="pricelist.stock_quantity"></number>
          </p>

          <p>
            <mt-button type="primary" size="small">立即购买</mt-button>&nbsp;
            <mt-button type="danger" size="small" @click="getshopcar">加入购物车</mt-button>
          </p>
        </div>
      </div>
    </div>

    <!-- 商品参数区域 -->
    <div class="mui-card">
      <div class="mui-card-header">商品参数</div>
      <div class="mui-card-content">
        <div class="mui-card-content-inner">
          <p>商品货号&nbsp;:{{pricelist.goods_no}}</p>
          <p>库存情况&nbsp;:{{pricelist.stock_quantity}}件</p>
          <p>上架时间&nbsp;:{{pricelist.add_time | myFilter}}</p>
        </div>
      </div>
      <div class="mui-card-footer">
        <mt-button type="primary" size="large" plain @click="img_duce(id)">图文介绍</mt-button>
        <mt-button type="danger" size="large" plain @click="img_details(id)">商品评论</mt-button>
      </div>
    </div>
  </div>
</template>

<script>
import number from "./subcomponents/Number.vue";
import swiper from "./subcomponents/Lunbotulist";

export default {
  data() {
    return {
      id: this.$route.params.id,
      lunbotuList: [],
      pricelist: {}, //详细价格等数据
      flagboll: false,
      getnum:1
    };
  },
  methods: {
    getLunbotu() {
      this.$axios.get("/api/getthumimages/" + this.id).then(result => {
        // console.log(result);
        this.lunbotuList = result.data.message;
        result.data.message.forEach(item => {
          item.img = item.src;
        });
      });
    },
    getlist() {
      //获得价格等信息
      this.$axios.get("/api/goods/getinfo/" + this.id).then(result => {
        this.pricelist = result.data.message[0];
        
      });
    },
    img_duce(id) {
      //图文介绍
      this.$router.push({ name: "Goodinfo", params: { id } });
    },
    img_details(id) {
      this.$router.push({ name: "Goodgesc", params: { id } });
    },
    getshopcar() {
      //加入购物车
      this.flagboll = !this.flagboll;
      var objcar={
        id:this.id,
        count:this.getnum,
        price:this.pricelist.sell_price,
        selected:true
      }
      this.$store.commit('addToCat',objcar)    //使用store里面的commit方法吧对象数据保存到vuex里面
      console.log(objcar)
    },
    beforeEnter(el) {
      el.style.transform = "translate(0,0)";
    },
    enter(el, done) {
      const ballPosition=this.$refs.myball.getBoundingClientRect();   //获得小球位置
      const badgePosition=document.getElementById('mybadge').getBoundingClientRect();    //获得徽标位置
      const T=(badgePosition.top-ballPosition.top)+'px'
      const X=(badgePosition.left-ballPosition.left)+'px'
      console.log(T,X)
      el.offsetWidth;
      el.style.transform = `translate(${X},${T})`
      console.log(el.style.transform)
      el.style.transition="all 1s ease"
      done();
    },
    afterEnter() {
        this.flagboll=!this.flagboll
    },
    number(count){
      // console.log(count)
      this.getnum=count
    }  
  },
  created() {
    this.getLunbotu();
    this.getlist();
  },
  components: {
    swiper,
    number
  }
};
</script>

<style scoped lang="scss">
.ball {
  width: 17px;
  height: 17px;
  border-radius: 50%;
  background-color: red;
  position: absolute;
  left: 149px;
  top: 362px;
  z-index: 999;
}
.price {
  color: #000;
  .now-price {
    color: red;
    font-size: 16px;
  }
  .old-price {
    font-size: 12px;
  }
}
.nummer {
  color: #000;
}
.mui-card-footer {
  display: block;
  button {
    margin-bottom: 15px;
  }
}
</style>
